-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #418 from Jujulego/feat/bind-decorator
Add bind decorator
- Loading branch information
Showing
9 changed files
with
237 additions
and
347 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
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,23 @@ | ||
import { SyncMutableRef, SyncRef } from './defs/index.js'; | ||
|
||
// Types | ||
export type BindDecorator<V> = <T>(target: ClassAccessorDecoratorTarget<T, V>, ctx: ClassAccessorDecoratorContext<T, V>) => ClassAccessorDecoratorResult<T, V>; | ||
|
||
// Decorator | ||
export function BindRef<V, D extends V>(ref: SyncRef<D> | SyncMutableRef<D>): BindDecorator<V> { | ||
return (_, ctx) => { | ||
const result: ClassAccessorDecoratorResult<unknown, D> = { | ||
get: ref.read, | ||
}; | ||
|
||
if ('mutate' in ref) { | ||
result.set = ref.mutate; | ||
} else { | ||
result.set = () => { | ||
throw new Error(`Cannot set ${String(ctx.name)}, it is bound to a readonly reference.`); | ||
}; | ||
} | ||
|
||
return result; | ||
}; | ||
} |
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
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,45 @@ | ||
import { BindRef } from '@/src/bind.js'; | ||
import { const$ } from '@/src/refs/const.js'; | ||
import { var$ } from '@/src/refs/var.js'; | ||
|
||
// Tests | ||
describe('bind$', () => { | ||
it('should bind getter to ref read method', () => { | ||
const value = const$(42); | ||
vi.spyOn(value, 'read'); | ||
|
||
const life = new class { | ||
@BindRef(value) | ||
accessor value: 42; | ||
}; | ||
|
||
expect(life.value).toBe(42); | ||
expect(value.read).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it('should throw on attempt to set a accessor bound to a read only reference', () => { | ||
const value = const$(42); | ||
vi.spyOn(value, 'read'); | ||
|
||
const life = new class { | ||
@BindRef(value) | ||
accessor value: number; | ||
}; | ||
|
||
expect(() => { life.value = 42; }).toThrow('Cannot set value, it is bound to a readonly reference'); | ||
}); | ||
|
||
it('should bind setter to ref mutate method', () => { | ||
const value = var$(0); | ||
vi.spyOn(value, 'mutate'); | ||
|
||
const life = new class { | ||
@BindRef(value) | ||
accessor value: number; | ||
}; | ||
|
||
life.value = 42; | ||
|
||
expect(value.mutate).toHaveBeenCalledWith(42); | ||
}); | ||
}); |
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
Oops, something went wrong.