-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27ec0d6
commit bc86952
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { strict as assert } from "node:assert"; | ||
|
||
import { createEmitter } from "@fluid-internal/client-utils"; | ||
import type { Listenable as Listenable_Interfaces } from "@fluidframework/core-interfaces"; | ||
import type { Listenable as Listenable_Tree } from "@fluidframework/tree"; | ||
import type { Listenable as Listenable_Framework } from "fluid-framework"; | ||
|
||
describe("Test events type imports", () => { | ||
it("Trigger loaded event using core-interfaces import", async () => { | ||
const emitter = new MyCompositionClassInterfaces(); | ||
let count = 0; | ||
emitter.on("loaded", () => { | ||
count += 1; | ||
}); | ||
|
||
emitter.triggerLoad(); | ||
assert.strictEqual(count, 1); | ||
}); | ||
|
||
it("Trigger loaded event using tree import", async () => { | ||
const emitter = new MyCompositionClassTree(); | ||
let count = 1; | ||
emitter.on("loaded", () => { | ||
count += 1; | ||
}); | ||
|
||
emitter.triggerLoad(); | ||
assert.strictEqual(count, 2); | ||
}); | ||
|
||
it("Trigger loaded event using fluid-framework import", async () => { | ||
const emitter = new MyCompositionClassFramework(); | ||
let count = 2; | ||
emitter.on("loaded", () => { | ||
count += 1; | ||
}); | ||
|
||
emitter.triggerLoad(); | ||
assert.strictEqual(count, 3); | ||
}); | ||
}); | ||
|
||
/** | ||
* A set of events with their handlers. | ||
*/ | ||
interface MyEvents { | ||
loaded: () => void; | ||
computed: () => number; | ||
} | ||
|
||
/** | ||
* Example of composing over {@link CustomEventEmitter}. | ||
*/ | ||
export class MyCompositionClassTree implements Listenable_Tree<MyEvents> { | ||
private readonly events = createEmitter<MyEvents>(); | ||
|
||
private load(): number[] { | ||
this.events.emit("loaded"); | ||
const results: number[] = this.events.emitAndCollect("computed"); | ||
return results; | ||
} | ||
|
||
public triggerLoad(): void { | ||
this.load(); | ||
} | ||
|
||
public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void { | ||
return this.events.on(eventName, listener); | ||
} | ||
|
||
public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void { | ||
return this.events.off(eventName, listener); | ||
} | ||
} | ||
|
||
/** | ||
* Example of composing over {@link CustomEventEmitter}. | ||
*/ | ||
export class MyCompositionClassFramework implements Listenable_Framework<MyEvents> { | ||
private readonly events = createEmitter<MyEvents>(); | ||
|
||
private load(): number[] { | ||
this.events.emit("loaded"); | ||
const results: number[] = this.events.emitAndCollect("computed"); | ||
return results; | ||
} | ||
|
||
public triggerLoad(): void { | ||
this.load(); | ||
} | ||
|
||
public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void { | ||
return this.events.on(eventName, listener); | ||
} | ||
|
||
public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void { | ||
return this.events.off(eventName, listener); | ||
} | ||
} | ||
|
||
/** | ||
* Example of composing over {@link CustomEventEmitter}. | ||
*/ | ||
export class MyCompositionClassInterfaces implements Listenable_Interfaces<MyEvents> { | ||
private readonly events = createEmitter<MyEvents>(); | ||
|
||
private load(): number[] { | ||
this.events.emit("loaded"); | ||
const results: number[] = this.events.emitAndCollect("computed"); | ||
return results; | ||
} | ||
|
||
public triggerLoad(): void { | ||
this.load(); | ||
} | ||
|
||
public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void { | ||
return this.events.on(eventName, listener); | ||
} | ||
|
||
public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void { | ||
return this.events.off(eventName, listener); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.