Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RiskyDiceRoller #23982

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { DiceRollerFactory } from "./diceRoller.js";

const diceRollerId = "dice-roller";
const diceRollerRegistryKey = "dice-roller";
const diceRollerFactory = new DiceRollerFactory();

export class DiceRollerContainerRuntimeFactory implements IRuntimeFactory {
public get IRuntimeFactory(): IRuntimeFactory {
Expand All @@ -32,6 +31,8 @@ export class DiceRollerContainerRuntimeFactory implements IRuntimeFactory {
containerRuntime: IContainerRuntime,
): Promise<FluidObject> => getDataStoreEntryPoint(containerRuntime, diceRollerId);

const diceRollerFactory = new DiceRollerFactory(() => runtime.dispose());

const runtime = await loadContainerRuntime({
context,
registryEntries: new Map([[diceRollerRegistryKey, Promise.resolve(diceRollerFactory)]]),
Expand Down
20 changes: 15 additions & 5 deletions examples/view-integration/external-views/src/diceRoller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ const diceValueKey = "dice-value";
/**
* The DiceRoller is our data object that implements the IDiceRoller interface.
*/
class DiceRoller implements IDiceRoller {
class RiskyDiceRoller implements IDiceRoller {
private readonly _events = new TypedEventEmitter<IDiceRollerEvents>();
public get events(): IEventProvider<IDiceRollerEvents> {
return this._events;
}

public constructor(private readonly map: ISharedMap) {
public constructor(
private readonly map: ISharedMap,
private readonly disposeRuntime: () => void,
) {
this.map.on("valueChanged", (changed: IValueChanged) => {
if (changed.key === diceValueKey) {
this._events.emit("diceRolled");
Expand All @@ -51,7 +54,12 @@ class DiceRoller implements IDiceRoller {

public readonly roll = () => {
const rollValue = Math.floor(Math.random() * 6) + 1;
this.map.set(diceValueKey, rollValue);
if (rollValue === 1) {
console.log("Snake eyes!");
this.disposeRuntime();
} else {
this.map.set(diceValueKey, rollValue);
}
};
}

Expand All @@ -64,6 +72,8 @@ export class DiceRollerFactory implements IFluidDataStoreFactory {
return this;
}

public constructor(private readonly disposeRuntime: () => void) {}

public async instantiateDataStore(
context: IFluidDataStoreContext,
existing: boolean,
Expand All @@ -75,13 +85,13 @@ export class DiceRollerFactory implements IFluidDataStoreFactory {
existing,
async () => {
map ??= (await runtime.getChannel(mapId)) as ISharedMap;
return new DiceRoller(map);
return new RiskyDiceRoller(map, this.disposeRuntime);
},
);

if (!existing) {
map = runtime.createChannel(mapId, mapFactory.type) as ISharedMap;
map.set(diceValueKey, 1);
map.set(diceValueKey, 6);
map.bindToContext();
}

Expand Down
Loading