-
-
Notifications
You must be signed in to change notification settings - Fork 258
/
Transferable.ts
124 lines (115 loc) · 3.07 KB
/
Transferable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @since 1.0.0
*/
import * as ParseResult from "@effect/schema/ParseResult"
import * as Schema from "@effect/schema/Schema"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import { dual } from "effect/Function"
import * as Option from "effect/Option"
/**
* @since 1.0.0
* @category models
*/
export interface CollectorService {
readonly addAll: (_: Iterable<globalThis.Transferable>) => Effect.Effect<void>
readonly unsafeAddAll: (_: Iterable<globalThis.Transferable>) => void
readonly read: Effect.Effect<ReadonlyArray<globalThis.Transferable>>
readonly unsafeRead: () => ReadonlyArray<globalThis.Transferable>
readonly unsafeClear: () => void
readonly clear: Effect.Effect<void>
}
/**
* @since 1.0.0
* @category tags
*/
export class Collector extends Context.Tag("@effect/platform/Transferable/Collector")<
Collector,
CollectorService
>() {}
/**
* @since 1.0.0
* @category constructors
*/
export const unsafeMakeCollector = (): CollectorService => {
const tranferables: Array<globalThis.Transferable> = []
const unsafeAddAll = (transfers: Iterable<globalThis.Transferable>): void => {
for (const transfer of transfers) {
tranferables.push(transfer)
}
}
const unsafeRead = (): ReadonlyArray<globalThis.Transferable> => tranferables
const unsafeClear = (): void => {
tranferables.length = 0
}
return Collector.of({
unsafeAddAll,
addAll: (transferables) => Effect.sync(() => unsafeAddAll(transferables)),
unsafeRead,
read: Effect.sync(unsafeRead),
unsafeClear,
clear: Effect.sync(unsafeClear)
})
}
/**
* @since 1.0.0
* @category constructors
*/
export const makeCollector: Effect.Effect<CollectorService> = Effect.sync(unsafeMakeCollector)
/**
* @since 1.0.0
* @category accessors
*/
export const addAll = (tranferables: Iterable<globalThis.Transferable>): Effect.Effect<void> =>
Effect.flatMap(
Effect.serviceOption(Collector),
Option.match({
onNone: () => Effect.void,
onSome: (_) => _.addAll(tranferables)
})
)
/**
* @since 1.0.0
* @category schema
*/
export const schema: {
<I>(
f: (_: I) => Iterable<globalThis.Transferable>
): <A, R>(self: Schema.Schema<A, I, R>) => Schema.Schema<A, I, R>
<A, I, R>(
self: Schema.Schema<A, I, R>,
f: (_: I) => Iterable<globalThis.Transferable>
): Schema.Schema<A, I, R>
} = dual(2, <A, I, R>(
self: Schema.Schema<A, I, R>,
f: (_: I) => Iterable<globalThis.Transferable>
) =>
Schema.transformOrFail(
Schema.encodedSchema(self),
self,
{ strict: true, decode: ParseResult.succeed, encode: (i) => Effect.as(addAll(f(i)), i) }
))
/**
* @since 1.0.0
* @category schema
*/
export const ImageData: Schema.Schema<ImageData> = schema(
Schema.Any,
(_) => [(_ as ImageData).data.buffer]
)
/**
* @since 1.0.0
* @category schema
*/
export const MessagePort: Schema.Schema<MessagePort> = schema(
Schema.Any,
(_) => [_ as MessagePort]
)
/**
* @since 1.0.0
* @category schema
*/
export const Uint8Array: Schema.Schema<Uint8Array> = schema(
Schema.Uint8ArrayFromSelf,
(_) => [_.buffer]
)