-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
watch
and unwatch
commands
- Loading branch information
Showing
13 changed files
with
249 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { convertStringToBuffer } from '../commands-utils/convertStringToBuffer' | ||
|
||
export function unwatch() { | ||
this.dirty = false | ||
this.watching.clear() | ||
return 'OK' | ||
} | ||
|
||
export function unwatchBuffer() { | ||
const val = unwatch.call(this) | ||
return convertStringToBuffer(val) | ||
} |
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,16 @@ | ||
import { convertStringToBuffer } from '../commands-utils/convertStringToBuffer' | ||
|
||
export function watch(...keys) { | ||
if (!this.dirty) { | ||
for (const key of keys) { | ||
this.watching.add(key) | ||
} | ||
} | ||
|
||
return 'OK' | ||
} | ||
|
||
export function watchBuffer(...keys) { | ||
const val = watch.apply(this, keys) | ||
return convertStringToBuffer(val) | ||
} |
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
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,52 @@ | ||
import Redis from 'ioredis' | ||
|
||
describe('watching', () => { | ||
let redis | ||
|
||
beforeEach(() => { | ||
redis = new Redis() | ||
}) | ||
|
||
afterEach(() => { | ||
redis.disconnect() | ||
}) | ||
|
||
describe('watch', () => { | ||
it('should set the watched keys', async () => { | ||
expect(await redis.watch('foo', 'bar')).toBe('OK') | ||
expect(redis.watching).toContain('foo', 'bar') | ||
expect(redis.dirty).toBe(false) | ||
}) | ||
|
||
it('should mark the client as dirty if a key is modified', async () => { | ||
expect(await redis.watch('foo', 'bar')).toBe('OK') | ||
await redis.set('foo', '') | ||
expect(redis.dirty).toBe(true) | ||
}) | ||
|
||
it('should should not add watched key if dirty', async () => { | ||
expect(await redis.watch('foo')).toBe('OK') | ||
await redis.set('foo', '') | ||
expect(redis.dirty).toBe(true) | ||
expect(await redis.watch('bar')).toBe('OK') | ||
expect(redis.watching).toEqual(new Set(['foo'])) | ||
}) | ||
}) | ||
|
||
describe('unwatch', () => { | ||
it('should unwatch all watched keys', async () => { | ||
expect(await redis.watch('foo', 'bar')).toBe('OK') | ||
expect(redis.watching).toContain('foo', 'bar') | ||
expect(await redis.unwatch()).toBe('OK') | ||
expect(redis.watching.size).toBe(0) | ||
}) | ||
|
||
it('should mark the client as not dirty', async () => { | ||
expect(await redis.watch('foo', 'bar')).toBe('OK') | ||
await redis.set('foo', '') | ||
expect(redis.dirty).toBe(true) | ||
expect(await redis.unwatch()).toBe('OK') | ||
expect(redis.dirty).toBe(false) | ||
}) | ||
}) | ||
}) |
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,22 @@ | ||
import Redis from 'ioredis' | ||
|
||
// eslint-disable-next-line import/no-relative-parent-imports | ||
import { runTwinSuite } from '../../../test-utils' | ||
|
||
runTwinSuite('unwatch', (command, equals) => { | ||
describe(command, () => { | ||
let redis | ||
|
||
beforeEach(() => { | ||
redis = new Redis() | ||
}) | ||
|
||
afterEach(() => { | ||
redis.disconnect() | ||
}) | ||
|
||
it('should return OK', async () => { | ||
expect(equals(await redis[command](), 'OK')).toBe(true) | ||
}) | ||
}) | ||
}) |
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,22 @@ | ||
import Redis from 'ioredis' | ||
|
||
// eslint-disable-next-line import/no-relative-parent-imports | ||
import { runTwinSuite } from '../../../test-utils' | ||
|
||
runTwinSuite('watch', (command, equals) => { | ||
describe(command, () => { | ||
let redis | ||
|
||
beforeEach(() => { | ||
redis = new Redis() | ||
}) | ||
|
||
afterEach(() => { | ||
redis.disconnect() | ||
}) | ||
|
||
it('should return OK', async () => { | ||
expect(equals(await redis[command]('foo', 'bar'), 'OK')).toBe(true) | ||
}) | ||
}) | ||
}) |
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