Skip to content

Commit

Permalink
Initial player svc implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyfran committed Jul 27, 2024
1 parent 694bf0c commit 9fae6a3
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"@effect/schema": "^0.67.18",
"effect": "^3.2.8",
"effect": "^3.5.8",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
14 changes: 10 additions & 4 deletions packages/components/effect-bridge/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cause, Effect, Exit, Match, Stream } from "effect";
import { Cause, Effect, Exit, Match, Sink, Stream } from "effect";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";

type RunEffectCallback = () => void;
Expand Down Expand Up @@ -179,8 +179,12 @@ export const useStream = <TResult, TError>(
Match.tag("initial", () => Effect.void),
Match.tag("success", (stream) =>
stream.result.pipe(
Stream.runForEachChunk((albums) =>
Effect.sync(() => setResult((prev) => [...prev, ...albums])),
Stream.run(
Sink.forEach((item) =>
Effect.sync(() => {
setResult((current) => [...current, item]);
}),
),
),
),
),
Expand All @@ -189,7 +193,9 @@ export const useStream = <TResult, TError>(
),
Match.exhaustive,
)(streamEffectState),
);
).then(() => {
console.log("Stream effect completed");
});
}, [streamEffectState, matcher]);

return [
Expand Down
2 changes: 1 addition & 1 deletion packages/components/effect-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"effect": "^3.2.8"
"effect": "^3.5.8"
},
"devDependencies": {
"@types/react": "^18.2.66",
Expand Down
2 changes: 1 addition & 1 deletion packages/components/state/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@echo/components-effect-bridge": "^1.0.0",
"@echo/core-types": "^1.0.0",
"@echo/services-bootstrap": "^1.0.0",
"effect": "^3.2.8",
"effect": "^3.5.8",
"jotai": "^2.8.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"dependencies": {
"@effect/schema": "^0.67.18",
"effect": "^3.2.8"
"effect": "^3.5.8"
}
}
10 changes: 8 additions & 2 deletions packages/core/types/src/services/player.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, Effect } from "effect";
import type { Album } from "../model";
import { Context, Effect, Stream } from "effect";
import type { Album, PlayerState } from "../model";

/**
* Service that provides a way to interact with the player and its state.
Expand All @@ -10,6 +10,12 @@ export type Player = {
* the playback to the appropriate media provider.
*/
readonly playAlbum: (album: Album) => Effect.Effect<void>;

/**
* Returns a stream that emits the current player state and any subsequent
* changes to it.
*/
readonly observe: Effect.Effect<Stream.Stream<PlayerState>>;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/infrastructure/broadcast-channel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"dependencies": {
"@echo/core-types": "^1.0.0",
"effect": "^3.2.8"
"effect": "^3.5.8"
}
}
2 changes: 1 addition & 1 deletion packages/infrastructure/browser-crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"dependencies": {
"@echo/core-types": "^1.0.0",
"effect": "^3.2.8"
"effect": "^3.5.8"
}
}
2 changes: 1 addition & 1 deletion packages/infrastructure/dexie-database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"@echo/core-strings": "^1.0.0",
"@echo/core-types": "^1.0.0",
"dexie": "^4.0.7",
"effect": "^3.2.8"
"effect": "^3.5.8"
}
}
2 changes: 1 addition & 1 deletion packages/infrastructure/mmb-metadata-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@echo/core-types": "^1.0.0",
"buffer": "^6.0.3",
"effect": "^3.2.8",
"effect": "^3.5.8",
"music-metadata": "^10.0.0",
"process": "^0.11.10"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/infrastructure/onedrive-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@echo/core-dates": "^1.0.0",
"@echo/core-types": "^1.0.0",
"@microsoft/microsoft-graph-client": "^3.0.7",
"effect": "^3.2.8"
"effect": "^3.5.8"
},
"devDependencies": {
"@microsoft/microsoft-graph-types": "^2.40.0"
Expand Down
5 changes: 3 additions & 2 deletions packages/services/bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"@echo/infrastructure-broadcast-channel": "^1.0.0",
"@echo/infrastructure-browser-crypto": "^1.0.0",
"@echo/infrastructure-dexie-database": "^1.0.0",
"@echo/services-library": "^1.0.0",
"@echo/infrastructure-mmb-metadata-provider": "^1.0.0",
"@echo/infrastructure-onedrive-provider": "^1.0.0",
"@echo/services-library": "^1.0.0",
"@echo/services-player": "^1.0.0",
"@echo/workers-media-provider": "^1.0.0",
"effect": "^3.2.8"
"effect": "^3.5.8"
}
}
2 changes: 2 additions & 0 deletions packages/services/bootstrap/src/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LibraryLive } from "@echo/services-library";
import { MmbMetadataProviderLive } from "@echo/infrastructure-mmb-metadata-provider";
import { LazyLoadedProviderLive } from "./loaders/provider";
import { AppConfigLive } from "./app-config";
import { PlayerLive } from "@echo/services-player";

/**
* Exports a layer that can provide all dependencies that are needed in the
Expand All @@ -17,6 +18,7 @@ import { AppConfigLive } from "./app-config";
export const MainLive = MediaProviderMainThreadBroadcastChannelLive.pipe(
Layer.provideMerge(MediaProviderWorkerBroadcastChannelLive),
Layer.provideMerge(BrowserCryptoLive),
Layer.provideMerge(PlayerLive),
Layer.provideMerge(LazyLoadedProviderLive),
Layer.provideMerge(LibraryLive),
Layer.provideMerge(DexieDatabaseLive),
Expand Down
2 changes: 1 addition & 1 deletion packages/services/library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"dependencies": {
"@echo/core-types": "^1.0.0",
"effect": "^3.2.8"
"effect": "^3.5.8"
}
}
2 changes: 2 additions & 0 deletions packages/services/player/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { PlayerStateRef } from "./src/state";
export { PlayerLive } from "./src/player";
15 changes: 15 additions & 0 deletions packages/services/player/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@echo/services-player",
"private": true,
"version": "1.0.0",
"description": "Contains the implementation for the Player service",
"main": "index.js",
"scripts": {
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@echo/core-types": "^1.0.0",
"effect": "^3.5.8"
}
}
49 changes: 49 additions & 0 deletions packages/services/player/src/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Player, type PlayerState } from "@echo/core-types";
import { Effect, Layer, Option, PubSub, Ref, Stream } from "effect";
import { PlayerStateRef } from "./state";

const PlayerLiveWithState = Layer.effect(
Player,
Effect.gen(function* () {
const state = yield* PlayerStateRef;
const statePubSub = yield* PubSub.dropping<PlayerState>({
capacity: 1,
replay: 1,
});

// Yield initial state to subscribers.
yield* statePubSub.publish(yield* state.get);

return Player.of({
playAlbum: (_album) =>
Effect.gen(function* () {
yield* Ref.update(state, (current) => ({
...current,
status: "playing" as const,
}));

yield* statePubSub.publish(yield* state.get);
}),
observe: Effect.sync(() =>
Stream.fromPubSub(statePubSub).pipe(
Stream.tap((state) => Effect.logInfo("Player state changed", state)),
Stream.ensuring(Effect.logInfo("Player state stream closed")),
),
),
});
}),
);

const PlayerStateLive = Layer.effect(
PlayerStateRef,
Ref.make({
comingUpTracks: [],
previouslyPlayedTracks: [],
currentTrack: Option.none(),
status: "stopped",
} as PlayerState),
);

export const PlayerLive = PlayerLiveWithState.pipe(
Layer.provide(PlayerStateLive),
);
9 changes: 9 additions & 0 deletions packages/services/player/src/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { PlayerState } from "@echo/core-types";
import { Context, Ref } from "effect";

/**
* Tag that can provide a ref to the current state of the player.
*/
export class PlayerStateRef extends Context.Tag(
"@echo/services-player/PlayerStateRef",
)<PlayerStateRef, Ref.Ref<PlayerState>>() {}
7 changes: 7 additions & 0 deletions packages/services/player/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tsconfig.json",
"include": [
"src",
"index.ts"
]
}
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@echo/components-state": "^1.0.0",
"@echo/core-types": "^1.0.0",
"@echo/services-bootstrap": "^1.0.0",
"effect": "^3.2.8",
"effect": "^3.5.8",
"jotai": "^2.8.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/workers/media-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"@echo/infrastructure-browser-crypto": "^1.0.0",
"@echo/services-bootstrap": "^1.0.0",
"@effect/schema": "^0.67.18",
"effect": "^3.2.8"
"effect": "^3.5.8"
}
}
22 changes: 7 additions & 15 deletions tools/plop-templates/infrastructure/template/package.json.hbs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
{
"name": "@echo/infrastructure-{{dashCase name}}",
"private": true,
"version": "1.0.0",
"description": "Contains the {{properCase name}} related infrastructure",
"main": "index.js",
"scripts": {
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@echo/core-types": "^1.0.0",
"effect": "^3.2.8"
}
}
{ "name": "@echo/infrastructure-{{dashCase name}}", "private": true, "version":
"1.0.0", "description": "Contains the
{{properCase name}}
related infrastructure", "main": "index.js", "scripts": { "lint": "eslint .
--ext ts,tsx --report-unused-disable-directives --max-warnings 0", "typecheck":
"tsc --noEmit" }, "dependencies": { "@echo/core-types": "^1.0.0", "effect":
"^3.5.8" } }
22 changes: 7 additions & 15 deletions tools/plop-templates/services/template/package.json.hbs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
{
"name": "@echo/services-{{dashCase name}}",
"private": true,
"version": "1.0.0",
"description": "Contains the implementation for the {{properCase name}} service",
"main": "index.js",
"scripts": {
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@echo/core-types": "^1.0.0",
"effect": "^3.2.8"
}
}
{ "name": "@echo/services-{{dashCase name}}", "private": true, "version":
"1.0.0", "description": "Contains the implementation for the
{{properCase name}}
service", "main": "index.js", "scripts": { "lint": "eslint . --ext ts,tsx
--report-unused-disable-directives --max-warnings 0", "typecheck": "tsc
--noEmit" }, "dependencies": { "@echo/core-types": "^1.0.0", "effect": "^3.5.8"
} }
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1063,10 +1063,10 @@ dot-case@^3.0.4:
no-case "^3.0.4"
tslib "^2.0.3"

effect@^3.2.8:
version "3.2.8"
resolved "https://registry.yarnpkg.com/effect/-/effect-3.2.8.tgz#2517f72cf7041605243f3a1ec65859b3b6b39cd5"
integrity sha512-W9xHCXg+CKwvNb+/vEsfiBxSqyOEAyjo7mz1M7Kq5ZtxBUyefOsQxc3oGME7bTcxD4OozWcR27Sk4iYxHERoZg==
effect@^3.5.8:
version "3.5.8"
resolved "https://registry.yarnpkg.com/effect/-/effect-3.5.8.tgz#63098d38f538facd4d8b0ca8b81717395c33f694"
integrity sha512-gGrRH3BgsUrfOXx4vbD4puRS+/IyK7JiERhuKnjVYurTVDj3J+lPI7raJB9/AAKr59SGEiV7+Sy59/Q/Vfo5mQ==

emoji-regex@^10.3.0:
version "10.3.0"
Expand Down

0 comments on commit 9fae6a3

Please sign in to comment.